package com.zhouyu.paydemo.unipay.qrcode.ZS.ATM;

import com.zhouyu.paydemo.unipay.qrcode.DemoBase;
import com.zhouyu.paydemo.unipay.sdk.QrcService;
import com.zhouyu.paydemo.unipay.sdk.SDKConfig;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
 * 用途：取现操作通知（付款方->银联）<br>
 * 日期： 2017-06<br>
 * 声明：以下代码只是为了方便商户测试而提供的样例代码，商户可以根据自己需要，按照技术文档编写。该代码仅供参考，不提供编码，性能，规范性等方面的保障<br>
 */
@SuppressWarnings("serial")
@Slf4j
public class CashWithdrawalNotify extends HttpServlet{

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
			String issCode = req.getParameter("issCode");
			String txnNo = req.getParameter("txnNo");
			String txnAmt = req.getParameter("txnAmt");
			
			String accNo = req.getParameter("accNo");
			String name = req.getParameter("name");
			String mobile = req.getParameter("mobile");
			String acctClass = req.getParameter("acctClass");
			String cardAttr = req.getParameter("cardAttr");
			
			String backUrl = req.getParameter("backUrl");
			
			Map<String, String> contentData = new HashMap<String, String>();
			contentData.put("version", "1.0.0");
			contentData.put("reqType", "0190000903");
			contentData.put("issCode", issCode);
			contentData.put("txnNo", txnNo);
			contentData.put("txnAmt", txnAmt);
			contentData.put("currencyCode", "156");
			
			//riskInfo必送，详细参考规范说明
			contentData.put("riskInfo", QrcService.base64Encode("{deviceID=123456999&deviceType=1&mobile=13525677809&accountIdHash=00000002&sourceIP=111.13.100.91}", DemoBase.encoding));
			
			Map<String, String> payerInfoMap = new HashMap<String, String>(); 
			payerInfoMap.put("accNo", accNo);
			if(null!=name && !"".equals(name)) payerInfoMap.put("name", name);
			payerInfoMap.put("mobile", mobile);//手机号必送
	        payerInfoMap.put("acctClass", acctClass);
	        payerInfoMap.put("cardAttr", cardAttr);
	        
	        //敏感信息不加密使用DemoBase.getPayerInfo方法
	        contentData.put("payerInfo", DemoBase.getPayerInfo(payerInfoMap,"UTF-8"));
	        
	        contentData.put("backUrl", backUrl);//2.5规范新增
	        
	        //如果对机构号issCode 配置了敏感信息加密，必须1.送encryptCertId 2.对payerInfo，payeeInfo的值整体加密
			//目前二维码系统要求所有接入均采用加密方式，使用正式机构号测试的时候参考如下方法上送
			//contentData.put("payerInfo", DemoBase.getPayerInfoWithEncrpyt(payerInfoMap,"UTF-8"));
			//contentData.put("encryptCertId", QrcService.getEncryptCertId());
	        
			//报文中certId,signature的值是在signData方法中获取并自动赋值的，只要证书配置正确即可。
			Map<String, String> reqData = QrcService.sign(contentData,DemoBase.encoding);		 
			String requestUrl = SDKConfig.getConfig().getQrcB2cIssBackTransUrl();
			//发送请求报文并接受同步应答（默认连接超时时间30秒，读取返回结果超时时间30秒）;这里调用signData之后，调用submitUrl之前不能对submitFromData中的键值对做任何修改，如果修改会导致验签不通过
			Map<String, String> rspData = QrcService.post(reqData,requestUrl,DemoBase.encoding);  
			
			if(!rspData.isEmpty()){
				if(QrcService.validate(rspData, DemoBase.encoding)){
					log.info("验证签名成功");
					String respCode = rspData.get("respCode") ;
					if(("00").equals(respCode)){

					}else{
						//其他应答码为失败请排查原因
						//TODO
					}
				}else{
					log.error("验证签名失败");
					//TODO 检查验证签名失败的原因
				}
			}else{
				//未返回正确的http状态
				log.error("未获取到返回报文或返回http状态码非200");
			}
			String reqMessage = DemoBase.genHtmlResult(reqData);
			String rspMessage = DemoBase.genHtmlResult(rspData);
			resp.getWriter().write("</br>请求报文:<br/>"+reqMessage+"<br/>" + "应答报文:</br>"+rspMessage+"");
			
			
			
		}
	
	
	public static void main(String[] args) throws IOException {
		SDKConfig.getConfig().loadPropertiesFromSrc();
		String payeeInfo_atm = "{name=交通银行&id=03010000&subId=012345678900001&subName=交通银行上海中山北路支行&amount=200000&termId=00000123}";
		String encrptDataStr = QrcService.encryptData(payeeInfo_atm, "UTF-8");
		System.out.println(encrptDataStr);
		String base64Str=QrcService.base64Encode(payeeInfo_atm, "UTF-8");
		System.out.println(base64Str);

		
	}
}

